package com.ckzp.core.model;

import cn.hutool.core.util.StrUtil;
import com.ckzp.core.json.JsonUtil;
import com.ckzp.core.util.DateUtil;
import com.ckzp.core.util.HtmlUtil;
import com.ckzp.core.util.StringUtil;
import com.jfinal.kit.StrKit;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 51594 on 2022/7/27.
 */
public class Datas<K, V> extends HashMap<K, V>
        implements Map<K, V>, Cloneable, Serializable {


    public Datas(Datas<? extends K, ? extends V> m) {
        super(m);
    }

    public Datas(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }


    @Override
    public V put(K key, V value) {
        return super.put(key, value);
    }

    public Datas() {
        super();
    }

    private boolean check = true;


    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }


    public static Datas n() {
        return new Datas();
    }

    public static Datas n(boolean check) {
        Datas t = new Datas();
        t.check = false;
        return t;
    }


    public Datas add(K keys, V values) {
        this.put(keys, values);
        return this;
    }

    public int getInt(String keys, int def) {
        String str = StringUtil.getString(this.get(keys), null);
        return StringUtil.getInt(str, def);
    }

    public int getInt(String keys) {
        return getInt(keys, 0);
    }

    /**
     * @param keys
     * @return
     */
    public boolean getBoolean(String keys) {
        String val = String.valueOf(this.get(keys));
        return "true".equals(val);
    }

    public Integer getInteger(String keys) {
        String val = String.valueOf(this.get(keys));
        return StrKit.isBlank(val) ? null : Integer.valueOf(val);
    }

    public Date getDate(String keys) {
        return getDate(keys, null);
    }


    public Date getTime(String keys) {
        return getTime(keys, null);
    }


    public Date getTime(String keys, Date def) {
        Date d = DateUtil.getTime(getStr(keys));
        return d == null ? def : d;
    }


    public Date getDate(String keys, Date def) {
        String values = getStr(keys);
        if (values == null || values.length() == 0) return def;
        Date d = DateUtil.getDate(values);
        return d == null ? def : d;
    }

    /**
     * 取列表
     *
     * @param keys
     * @param def
     * @return
     */
    public List getList(String keys, List def) {
        Object values = get(keys);
        if (values == null) return def;
        if (values instanceof List) return (List) values;
        return def;
    }

    /**
     * 取列表
     *
     * @param keys
     * @return
     */
    public List getList(String keys) {
        return getList(keys, null);
    }


    public long getLong(String keys) {
        return getLong(keys, 0);
    }


    public double getDouble(String keys) {
        return getDouble(keys, 0);
    }


    public double getDouble(String keys, double def) {
        String str = StringUtil.getString(this.get(keys), null);
        return StringUtil.getDouble(str, def);
    }

    public long getLong(String keys, long def) {
        String str = StringUtil.getString(this.get(keys), null);
        return StringUtil.getLong(str, def);
    }

    public String getStr(String keys, String def) {
        Object value = this.get(keys);
        if (value == null) return def;
        String values = String.valueOf(value);
        if (StrUtil.isBlank(values)) return def;
        return HtmlUtil.toHtmlInput(values);
    }

    public String getBaseStr(String keys, String def) {
        String values = String.valueOf(this.get(keys));
        if (values == null) return def;
        return values;
    }


    public String getBaseStr(String keys) {
        return getBaseStr(keys, "");
    }


    public String getEmojiStr(String keys) {
        return getEmoji(getBaseStr(keys, ""));
    }

    public static String getEmoji(String input) {
        if (input != null) {
            String patternStr = "[^\\u0000-\\uFFFF]";
            Pattern pattern = Pattern.compile(patternStr, Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(input);
            input = matcher.replaceAll("");
        }
        return input;
    }


    public Datas getDatas(String keys) {
        Object o = this.get(keys);
        if (o != null) {
            return JsonUtil.json2Datas(o.toString());
        } else {
            return new Datas();
        }
    }

    public String getStr(String keys) {
        return getStr(keys, "");
    }


    public String getArray(String keys) {
        Object o = this.get(keys);
        System.out.println(o);
        System.out.println("array" + (o instanceof Object[]));
        System.out.println("string" + o instanceof String);
        return getStr(keys, "");
    }

    @Override
    public String toString() {
        return super.toString();
    }

    public String toJson() {
        return JsonUtil.toJsonString(this);
    }
}
